package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("department")
public class DepartmentController {

    @Autowired
    private IDepartmentService service;

    @RequestMapping("")
    @PermissionName("部门列表")
    @RequiresPermissions("department:list")
    public String index(){
        return "department/list";
    }

    @RequestMapping("list")
    @ResponseBody
    @PermissionName("部门数据")
    @RequiresPermissions("department:data")
    public PageResult list(QueryObject qo){
        return service.query(qo);
    }
    @RequestMapping("save")
    @ResponseBody
    @PermissionName("部门保存")
    @RequiresPermissions("department:save")
    public JsonResult save(Department entity){
        JsonResult result = new JsonResult();
        try {
            service.save(entity);
        }catch(Exception e){
            e.printStackTrace();
            result = new JsonResult("保存操作失败");
        }
        return result;
    }
    @RequestMapping("update")
    @ResponseBody
    @PermissionName("部门更新")
    @RequiresPermissions("department:update")
    public JsonResult update(Department entity){
        JsonResult result = new JsonResult();
        try {
            service.update(entity);
        }catch(Exception e){
            e.printStackTrace();
            result = new JsonResult("编辑操作失败");
        }
        return result;
    }
    @RequestMapping("changeState")
    @ResponseBody
    @PermissionName("部门状态")
    @RequiresPermissions("department:changeState")
    public JsonResult changeState(Long id){
        JsonResult result = new JsonResult();
        try {
            service.changeState(id);
        }catch(Exception e){
            e.printStackTrace();
            result = new JsonResult("编辑操作失败");
        }
        return result;
    }

    @RequestMapping("queryDepts")
    @ResponseBody
    public List<Department> queryDepts(){
        return service.list();
    }
}
